package com.sample.android.sqlite;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class MySQLiteHelper extends SQLiteOpenHelper
{
	private static final String	DATABASE_NAME		= "sybe";
	private static final int	DATABASE_VERSION	= 1;

	// Database creation sql statement
	private List<String>		DATABASE_TABLES		= new ArrayList<String>();

	public MySQLiteHelper(Context context)
	{
		super(context, DATABASE_NAME, null, DATABASE_VERSION);

		// TABLE Utilisateur
		DATABASE_TABLES.add("CREATE TABLE `Utilisateur` ("
				+ "`_id` INTEGER PRIMARY KEY,"
				+ "`email` TEXT NOT NULL ," + "`password` TEXT NOT NULL ,"
				+ "`birthday` TEXT NULL DEFAULT NULL);");

		// TABLE Amis
//		DATABASE_TABLES
//				.add("CREATE  TABLE IF NOT EXISTS `sybe`.`Amis` ("
//						+ "`utilisateur` INT(11) NOT NULL ,"
//						+ "`contact` INT(11) NOT NULL ,"
//						+ "`Groupe_id` INT(11) NOT NULL ,"
//						+ "`Groupe_Utilisateur_id` INT(11) NOT NULL ,"
//						+ "PRIMARY KEY (`utilisateur`, `contact`, `Groupe_id`, `Groupe_Utilisateur_id`) ,"
//						+ "INDEX `fk_Amis_Utilisateur1_idx` (`contact` ASC) ,"
//						+ "INDEX `fk_Amis_Groupe1_idx` (`Groupe_id` ASC, `Groupe_Utilisateur_id` ASC) , "
//						+ "CONSTRAINT `fk_Amis_Utilisateur`"
//						+ "  FOREIGN KEY (`utilisateur` )"
//						+ "  REFERENCES `sybe`.`Utilisateur` (`_id` )"
//						+ "  ON DELETE NO ACTION"
//						+ "  ON UPDATE NO ACTION,"
//						+ "CONSTRAINT `fk_Amis_Utilisateur1`"
//						+ "  FOREIGN KEY (`contact` )"
//						+ "  REFERENCES `sybe`.`Utilisateur` (`_id` )"
//						+ "  ON DELETE NO ACTION"
//						+ "  ON UPDATE NO ACTION,"
//						+ "CONSTRAINT `fk_Amis_Groupe1`"
//						+ "  FOREIGN KEY (`Groupe_id` , `Groupe_Utilisateur_id` )"
//						+ "  REFERENCES `sybe`.`Groupe` (`_id` , `Utilisateur_id` )"
//						+ "  ON DELETE NO ACTION" + "  ON UPDATE NO ACTION);");
		// TABLE Dossier
		DATABASE_TABLES
				.add("CREATE  TABLE `Dossier` ("
						+ "`_id` INTEGER PRIMARY KEY ,"
						+ "`libelle` TEXT NULL DEFAULT NULL ,"
						+ "`Utilisateur_id` INTEGER NOT NULL ,"
						+ " FOREIGN KEY (`Utilisateur_id`)"
						+ " REFERENCES `Utilisateur` (`_id`)"
						+ " ON DELETE NO ACTION" + "  ON UPDATE NO ACTION);");
		// TABLE Lien
		DATABASE_TABLES.add("CREATE  TABLE `Lien` ("
				+ "`_id` INTEGER PRIMARY KEY AUTOINCREMENT," 
				+ "`url` TEXT NULL DEFAULT NULL ,"
				+ "`date_ajout_fr` TEXT NULL DEFAULT NULL ,"
				+ "`date_ajout_en` TEXT NULL DEFAULT NULL ,"
				+ "`Dossier_id` INTEGER NOT NULL ,"
				+ " FOREIGN KEY (`Dossier_id`)"
				+ " REFERENCES `Dossier` (`_id`)"
				+ " ON DELETE NO ACTION" + "  ON UPDATE NO ACTION);");
		// TABLE Groupe
//		DATABASE_TABLES.add("CREATE  TABLE IF NOT EXISTS `sybe`.`Groupe` ("
//				+ "`_id` INT(11) NOT NULL ,"
//				+ "`libelle` VARCHAR(45) NULL DEFAULT NULL ,"
//				+ "`Utilisateur_id` INT(11) NOT NULL ,"
//				+ "PRIMARY KEY (`_id`, `Utilisateur_id`) ,"
//				+ "INDEX `fk_Groupe_Utilisateur1_idx` (`Utilisateur_id` ASC) ,"
//				+ "CONSTRAINT `fk_Groupe_Utilisateur1`"
//				+ "  FOREIGN KEY (`Utilisateur_id` )"
//				+ "  REFERENCES `sybe`.`Utilisateur` (`_id` )"
//				+ "  ON DELETE NO ACTION" + "  ON UPDATE NO ACTION);");
		// TABLE Terminal
//		DATABASE_TABLES.add("CREATE  TABLE IF NOT EXISTS `sybe`.`Terminal` ("
//				+ "`principal` TINYINT(1) NULL DEFAULT NULL ,"
//				+ "`libelle` VARCHAR(45) NULL DEFAULT NULL ,"
//				+ "`Utilisateur_id` INT(11) NOT NULL ,"
//				+ "`Lien_id` INT(11) NOT NULL ,"
//				+ "PRIMARY KEY (`Utilisateur_id`, `Lien_id`) ,"
//				+ "INDEX `fk_Terminal_Lien1_idx` (`Lien_id` ASC) ,"
//				+ "CONSTRAINT `fk_Terminal_Utilisateur1`"
//				+ "  FOREIGN KEY (`Utilisateur_id` )"
//				+ "  REFERENCES `sybe`.`Utilisateur` (`_id` )"
//				+ "  ON DELETE NO ACTION" + "  ON UPDATE NO ACTION,"
//				+ "CONSTRAINT `fk_Terminal_Lien1`"
//				+ "  FOREIGN KEY (`Lien_id` )"
//				+ "  REFERENCES `sybe`.`Lien` (`_id` )"
//				+ "  ON DELETE NO ACTION" + "  ON UPDATE NO ACTION)"); 
		// Contraintes finales
//		DATABASE_TABLES.add("SET SQL_MODE=@OLD_SQL_MODE;"
//				+ "SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;"
//				+ "SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;");

	}

	@Override
	public void onCreate(SQLiteDatabase database)
	{
		for (String table : DATABASE_TABLES)
		{
			database.execSQL(table);
		}
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
	{
		Log.w(MySQLiteHelper.class.getName(),
				"Upgrading database from version " + oldVersion + " to "
						+ newVersion + ", which will destroy all old data");
		db.execSQL("DROP ALL TABLES FROM " + DATABASE_NAME);
		onCreate(db);
	}

}
